﻿using System;
using System.Threading.Tasks;
using Box2DX.Collision;
using Box2DX.Common;
using Box2DX.Dynamics;

namespace PhysicsProject.Entities.Models
{
    public class Environment
    {
        private bool _isRunning;

        public Environment()
        {
            var worldAABB = new AABB();
            worldAABB.LowerBound.Set(-100.0f,-100.0f);
            worldAABB.UpperBound.Set(100.0f,100.0f);

            var gravity = new Vec2(0.0f,-9.8f);

            var doSleep = true;

            Underlying = new World(worldAABB, gravity, doSleep);

            var ground = new BodyDef();
            ground.Position.Set(0.0f,-10.0f);
            
            var groundBody = Underlying.CreateBody(ground);

            var groundShapeDef = new PolygonDef();
            groundShapeDef.SetAsBox(50.0f,10.0f);
            groundBody.CreateShape(groundShapeDef);

        }

        public void Start()
        {
            _isRunning = true;
            Task.Run(new Action(Step));
        }

        private void Step()
        {
            while(_isRunning)
                Underlying.Step(60, 1, 0);
        }

        public void Stop()
        {
            _isRunning = false;
        }

        public void AddBox(Box boxy)
        {
            
        }

        public World Underlying { get; private set; }

        public float CurrentTemperature { get; set; }

        public Material MaterialType { get; set; }


    }
}
